package com.bluewave.goaltracker;

import com.bluewave.goaltracker.utils.Goal;

import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.widget.Toast;

public class NotUsedNotificationBroadcastReceiver extends BroadcastReceiver {

	private static final int DAILY_NOTIFICATION = 1;
	private static final int QUARTERLY_NOTIFICATION = 2;

	@Override
	public void onReceive(Context context, Intent intent) {

		int notification_type = intent.getIntExtra("notification_type",
				Goal.MONTHLY);
		Context mContext = context.getApplicationContext();

		String ns = Context.NOTIFICATION_SERVICE;
		NotificationManager mNotificationManager = (NotificationManager) context
				.getSystemService(ns);
		int icon = R.drawable.ic_status_bar;
		CharSequence tickerText = "Goal Coach Reminder";
		long when = System.currentTimeMillis();

		Notification notification = new Notification(icon, tickerText, when);
		CharSequence contentTitle = "Goal Coach Reminder";
		SharedPreferences pres = PreferenceManager.getDefaultSharedPreferences(context);
		boolean isSound = pres.getBoolean("alarm_sound", true);
		boolean isVibrate = pres.getBoolean("alarm_vibration", true);
		boolean isPopUp =  pres.getBoolean("alarm_pop_up", false);
		if (notification_type == Goal.MONTHLY) {

			Intent notificationIntent = new Intent(context, GoalListActivity.class);
			notificationIntent.putExtra("screen_type", Goal.MONTHLY);

			CharSequence contentText = "Hi! Time to update your goals' status";

			PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
					notificationIntent, 0);
			notification.setLatestEventInfo(mContext, contentTitle,
					contentText, contentIntent);
			notification.flags |= Notification.FLAG_AUTO_CANCEL;
			if(isSound){
				notification.defaults |= Notification.DEFAULT_SOUND;
			}
			
			if(isVibrate){
				notification.defaults |= Notification.DEFAULT_VIBRATE;
			}

			notification.ledARGB = 0xff33ccff;
			notification.ledOnMS = 300;
			notification.ledOffMS = 1000;
			notification.flags |= Notification.FLAG_SHOW_LIGHTS;

			mNotificationManager.notify(DAILY_NOTIFICATION, notification);
			if(isPopUp){
				showDialogActivity(mContext, contentTitle, contentText);
			}
		}
		else if (notification_type == Goal.NOT_USED_ALERT) {

			SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
			long last_used= prefs.getLong("last_used", System.currentTimeMillis());
			if(System.currentTimeMillis()-last_used>=30*AlarmManager.INTERVAL_DAY){
				Intent notificationIntent = new Intent(context, GoalListActivity.class);

				CharSequence contentText = "Hi! TIme to check your Goals in GoalCoach";

				PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
						notificationIntent, 0);
				notification.setLatestEventInfo(mContext, contentTitle,
						contentText, contentIntent);
				notification.flags |= Notification.FLAG_AUTO_CANCEL;
				if(isSound){
					notification.defaults |= Notification.DEFAULT_SOUND;
				}
				
				if(isVibrate){
					notification.defaults |= Notification.DEFAULT_VIBRATE;
				}

				notification.ledARGB = 0xff33ccff;
				notification.ledOnMS = 300;
				notification.ledOffMS = 1000;
				notification.flags |= Notification.FLAG_SHOW_LIGHTS;

				mNotificationManager.notify(QUARTERLY_NOTIFICATION, notification);
				if(isPopUp){
					showDialogActivity(mContext, contentTitle, contentText);
				}
			}
		}
	}
	
	private void showDialogActivity(Context context, CharSequence contentTitle, CharSequence contentText){
		Intent dialogActivityIntent = new Intent(context, DialogActivity.class);
		dialogActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		dialogActivityIntent.putExtra("title", contentTitle);
		dialogActivityIntent.putExtra("message", contentText);
		context.startActivity(dialogActivityIntent);		   
	}

}
